/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "stdio.h"
#include "string.h"
#include "stdbool.h"
#include "cmsis_os2.h"
#include "softbus_uart_485_main.h"
// #include "cmd_input.h"
#include "trans_uart_485.h"

#define CMD_DESC_INFO_LEN 32
#define SIGINT 2

typedef void(*CmdFunc)(void);
typedef struct {
    char desc[CMD_DESC_INFO_LEN];
    CmdFunc func;
} CmdNode;
static volatile bool bExit = false;
static void Exit(void);

static CmdNode g_cmdList[] = {
    { "NULL", NULL},        // 0
    { "CreateSessionServer_UART_485", T_CreateSessionServerUart485 },   
    { "RemoveSessionServer_UART_485", T_RemoveSessionServerUart485 },   
    { "Exit", Exit }, /* keep Exit at last */               // 12
};
static unsigned int g_cmdNum = sizeof(g_cmdList) / sizeof(CmdNode);

static void Exit(void)
{
    bExit = true;
}

static void Helper(void)
{
    printf("*******UART 485 Task Tool Command List*******\r\n");
    for (unsigned int i = 0; i < g_cmdNum; i++) {
        printf("*     %02d - %-28s     *\r\n", i, g_cmdList[i].desc);
    }
    printf("*********************************************\r\n");
}


void softbus_uart_485_task(void)
{
    unsigned int index;
    char char_buf;

    printf("******softbus_uart_485_task delay 1S******\r\n");
    osDelay(1000U);
    printf("******softbus_uart_485_task begin******\r\n");

    T_CreateSessionServerUart485();
    
    do {
        Helper();
        index = GetInputNumber("Please input cmd index:");
        if (bExit) 
            break;
        if (index < 0 || index >= g_cmdNum) {
            printf("invalid cmd: %d\r\n", index);
            continue;
        }
        
        if(index != 0)
        {
            printf("\r\ncmd: %d\r\n", index);
            printf("\r\nExecute: %s\r\n", g_cmdList[index].desc);
            g_cmdList[index].func();
        }
        osDelay(200U);
    } while ((index + 1) != g_cmdNum && bExit == false);

    T_RemoveSessionServerUart485();

    printf("******softbus_uart_485_task exit******\r\n");
    osThreadExit();
}

#define SOFTBUS_UART_485_TASK_STACK_SIZE 4096
#define SOFTBUS_UART_485_TASK_PRIO 20

void AppUart485TaskStart(void)
{
    printf("AppLedTaskStart in\r\n");

    osThreadAttr_t attr;

    attr.name = "UART_485_TASK_NAME";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = SOFTBUS_UART_485_TASK_STACK_SIZE;
    attr.priority = SOFTBUS_UART_485_TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)softbus_uart_485_task, NULL, &attr) == NULL) {
        printf("Failed to create thread!\n");
    }
}
    

